<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once 'constants.php';

class Mysql{
	public function __construct()
	{
		mysql_connect(DB_SERVER,DB_USER,DB_PASS);
		mysql_select_db(DB_NAME);
	}
	
	public function __destruct() {
    	mysql_close();
    }
public function getRows($tablename,$fields,$table_id_name,$fks,$id){
		$fields_str = implode(',',$fields);
    	$sql = <<<QUERY
    	SELECT 
    	$fields_str
    	FROM
    	$tablename	
QUERY;
if($id != ""){ $sql .= "WHERE $table_id_name = $id";}
//echo $sql;
    	$rows = array();
		$requete = mysql_query($sql) or die(mysql_error());
    	
		while ($row = mysql_fetch_assoc($requete)){
				$row_fks = array();
    			$loop_id = $row[$table_id_name];
				foreach($fks as $key => $value){
					$current_id = $value['current_id'];
					$referenced_column = $value['referenced_column'];
					$referenced_table = $value['referenced_table'];
					$referenced_id = $value['referenced_id'];
					$in_out = $value['in_out'];
    				$list_data_fk = $this->getRow_fks($referenced_column, $referenced_table, $tablename, $referenced_id, $current_id, $table_id_name,$loop_id,$in_out);
					//$row[$referenced_id] = $list_data_fk;
					$row_fks[$current_id] = $list_data_fk;
					//print_r("<pre>");print_r($row);print_r("</pre>");
				}
				$row_to_send['fks'] = $row_fks;
				$row_to_send['fields'] = $row;  
				array_push($rows,$row_to_send);
				//print_r("<pre>");print_r($rows);print_r("</pre>");
		}
		return $rows;
}

public function getRow_fks($referenced_column,$referenced_table,$tablename,$referenced_id,$current_id,$table_id,$loop_id,$in_out){
	$sql = <<<SQL
    SELECT a.$referenced_column,a.$referenced_id
	FROM $referenced_table a, $tablename b 
	WHERE  a.$referenced_id = b.$current_id 
SQL;
	/*
	$sql = <<<SQL
    SELECT a.$referenced_column,a.$referenced_id
	FROM $referenced_table a, $tablename b
	WHERE 1
SQL;
*/
		
	if($in_out == "in" ) { $sql .= "AND b.$table_id = $loop_id";}
	if($in_out == "out" ) { $sql .= "AND a.$table_id = $loop_id";}
	if($in_out == "all" ) { $sql = "SELECT $referenced_column,$referenced_id FROM $referenced_table";}
	if($in_out == "new" ) { $sql = "SELECT $referenced_column,$referenced_id FROM $referenced_table WHERE $current_id = $loop_id"; }
	
	//echo $sql;
	$rows = array();
	$requete = mysql_query($sql) or die(mysql_error());
	
	while ($row = mysql_fetch_assoc($requete)){
		$rows[$row[$referenced_id]] = $row[$referenced_column];
	}
	//print_r("<pre>");print_r($rows);print_r("</pre>");
	return $rows;
}

public function editRow($tablename,$edited_data,$id,$table_id){
	$strings_array = array();
	foreach ($edited_data as $key => $value) {
    	array_push($strings_array,$key." = '".$value."'");
	}
	$data_string = implode(',',$strings_array);
	
	$sql = <<<SQL
		UPDATE $tablename
		SET
			$data_string
		WHERE $table_id = $id;
SQL;

	//echo $sql;
	$result = mysql_query($sql);
}


public function addRow($tablename,$data){
	$glue = ',';
	$fields_string = implode($glue,array_keys($data));
	$glue = '\',\'';
	$data_string = implode($glue,array_values($data));

	$sql = <<<SQL
	INSERT INTO
	$tablename
	($fields_string)
	VALUES
	('$data_string')
SQL;
	echo $sql;
    $requete = mysql_query($sql) or die(mysql_error());
}

public function getForeignKeysRows($tablename,$fks){
	$row_fks = array();
	foreach($fks as $key => $value){
		$current_id = $value['current_id'];
		$referenced_column = $value['referenced_column'];
		$referenced_table = $value['referenced_table'];
		$referenced_id = $value['referenced_id'];
		$list_data_fk = $this->getRow_fks($referenced_column, $referenced_table, $tablename, $referenced_id, $current_id, "Not Uesd","Not Used","all");
		$row_fks[$referenced_id] = $list_data_fk;
	}
	return $row_fks;
}
}